package pl.edu.agh.xls2html.htmlelements;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class CellStyle {

	private static final String DEFAULT_COLOR = "000000";

	private HSSFWorkbook wb;

	private String leftBorderStyle = "";
	private String topBorderStyle = "";
	private String rightBorderStyle = "";
	private String bottomBorderStyle = "";
	private String backgroundColor = "";
	private String textAlign = "";
	private int cellWidth = 0;
	private int cellHeight = 0;

	public CellStyle(String leftBorderStyle, String topBorderStyle,
			String rightBorderStyle, String bottomBorderStyle,
			String backgroundColor, String texAlign, int cellWidth,
			int cellHeight) {

		this.topBorderStyle = topBorderStyle;
		this.bottomBorderStyle = bottomBorderStyle;
		this.leftBorderStyle = leftBorderStyle;
		this.rightBorderStyle = rightBorderStyle;
		this.backgroundColor = backgroundColor;
		this.textAlign = texAlign;
		this.cellWidth = cellWidth;
		this.cellHeight = cellHeight;

	}

	public String getBackgroundColor() {
		return backgroundColor;
	}

	public int getCellWidth() {
		return cellWidth;
	}

	public int getCellHeight() {
		return cellHeight;
	}

	public String getLeftBorderStyle() {
		return leftBorderStyle;
	}

	public String getRightBorderStyle() {
		return rightBorderStyle;
	}

	public String getTopBorderStyle() {
		return topBorderStyle;
	}

	public String getBottomBorderStyle() {
		return bottomBorderStyle;
	}

	public String getTextAlign() {
		return textAlign;
	}

	private String getBorderStyle(int style, int color) {
		String borderStyle = "none";
		String borderThickness = "1px";
		String borderColor = getColor(color);
		if (style == HSSFCellStyle.BORDER_DASH_DOT
				|| style == HSSFCellStyle.BORDER_DASH_DOT_DOT
				|| style == HSSFCellStyle.BORDER_DASHED
				|| style == HSSFCellStyle.BORDER_DOTTED)
			borderStyle = "dotted";
		if (style == HSSFCellStyle.BORDER_NONE)
			borderStyle = "none";

		if (style == HSSFCellStyle.BORDER_HAIR
				|| style == HSSFCellStyle.BORDER_THIN) {
			borderStyle = "solid";
			borderThickness = "1px";
		}

		if (style == HSSFCellStyle.BORDER_MEDIUM) {
			borderStyle = "solid";
			borderThickness = "2px";
		}
		if (style == HSSFCellStyle.BORDER_THICK) {
			borderStyle = "solid";
			borderThickness = "3px";
		}

		borderColor = '#' + borderColor;

		if (borderStyle == "none") {
			borderThickness = "";
			borderColor = "";
		}

		return borderStyle + " " + borderThickness + " " + borderColor;
	}

	private String leadingZero(String col) {
		if (col.length() != 1)
			return col;

		return "0" + col;

	}

	public String getColor(int c) {
		HSSFPalette pal = wb.getCustomPalette();
		HSSFColor col = pal.getColor(c);
		if (col == null) {
			return DEFAULT_COLOR;
		}

		short[] ct = col.getTriplet();
		return leadingZero(Integer.toHexString(ct[0]))
				+ leadingZero(Integer.toHexString(ct[1]))
				+ leadingZero(Integer.toHexString(ct[2]));
	}

	private String getCellBackgroundColor(HSSFCellStyle style) {
		String bgcol = getColor(style.getFillBackgroundColor());
		String fgcol = getColor(style.getFillForegroundColor());

		if (!fgcol.equals(DEFAULT_COLOR))
			bgcol = fgcol;

		if (bgcol.equals(DEFAULT_COLOR))
			bgcol = "FFFFFF";

		return bgcol;
	}

	private String getCellAlignment(HSSFCellStyle style) {
		int align = style.getAlignment();
		String alignment = "";
		switch (align) {
		case HSSFCellStyle.ALIGN_CENTER:
			alignment = "center";
			break;
		case HSSFCellStyle.ALIGN_RIGHT:
			alignment = "right";
			break;
		default:
			alignment = "left";
			break;
		}

		return alignment;
	}

	public CellStyle(HSSFCell cell, HSSFWorkbook wb) {
		this.wb = wb;
		HSSFCellStyle style = cell.getCellStyle();

		leftBorderStyle = getBorderStyle(style.getBorderLeft(), style
				.getLeftBorderColor());

		HSSFCell c = cell.getSheet().getRow(cell.getRowIndex()).getCell(
				cell.getColumnIndex() - 1);
		if (c != null)
			if (c.getCellStyle().getBorderRight() == style.getBorderLeft())
				leftBorderStyle = "none";

		topBorderStyle = getBorderStyle(style.getBorderTop(), style
				.getTopBorderColor());

		HSSFRow r = cell.getSheet().getRow(cell.getRowIndex() - 1);
		if (r != null) {
			c = r.getCell(cell.getColumnIndex());
			if (c != null)
				if (c.getCellStyle().getBorderBottom() == style.getBorderTop())
					topBorderStyle = "none";
		}

		rightBorderStyle = getBorderStyle(style.getBorderRight(), style
				.getRightBorderColor());
		bottomBorderStyle = getBorderStyle(style.getBorderBottom(), style
				.getBottomBorderColor());

		cellWidth = cell.getSheet().getColumnWidth(cell.getColumnIndex())
				/ CellProperties.SCALE_WIDTH;
		cellHeight = cell.getSheet().getDefaultRowHeight()
				/ CellProperties.SCALE_HEIGHT + 2;
		r = cell.getSheet().getRow(cell.getRowIndex());
		if (r != null)
			cellHeight = r.getHeight() / CellProperties.SCALE_HEIGHT + 2;

		backgroundColor = getCellBackgroundColor(style);
		textAlign = getCellAlignment(style);

	}

}
